package insert;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

/**
 * Servlet implementation class ModifierMDPServlet
 */
public class ModifierMDPServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ModifierMDPServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		RequestDispatcher dis;
		Connection con = null;
		
		Integer membre = (Integer)session.getAttribute("user_id");
		if(membre == null){
			dis = request.getRequestDispatcher("/login.jsp");
			dis.forward(request, response);
			return;
		}
		
		String mdp = request.getParameter("mdp");
		String confirmmdp = request.getParameter("confirmmdp");
		
		List<String> erreur = new LinkedList<String>();
		
		if(mdp.isEmpty() || confirmmdp.isEmpty()) {
			erreur.add("Un des champs obligatoire n'est pas renseign�");
		}
		
		if(!mdp.equals(confirmmdp)) erreur.add("La mot de passe et la confirmation du mot de passe ne sont pas �gales");
		
		if(!erreur.isEmpty()){ //Donn�es du formulaire incorrectes
			request.setAttribute("erreur", erreur);
			dis=request.getRequestDispatcher("modifiermdp.jsp");
			dis.forward(request,response);
		}else{ //Donn�es correctes, envoie de la requete SQL
			PreparedStatement pstmt;
			try {
				Context namingContext = new InitialContext();
				DataSource dataSource = (DataSource)namingContext.lookup("java:comp/env/jdbc/MiniIBay");
				con = dataSource.getConnection();
				
				pstmt = con.prepareStatement("UPDATE `membre` SET `password`=? WHERE `id_membre`=?");
				pstmt.setString(1, mdp);
				pstmt.setInt(2, membre);
				pstmt.executeUpdate();
				
				response.sendRedirect("/MiniIBay/profile");
			} catch (NamingException e) {
				response.sendRedirect("../home");
			} catch (SQLException e) {
				response.sendRedirect("../home");
			}finally{
				if(con != null){
					try {
						con.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
	}

}
